package com.pro.addresslist.ui.activity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.pro.addresslist.R;
import com.pro.addresslist.dal.bll.ContactInfoBLL;
import com.pro.addresslist.dal.model.ContactInfo;
import com.pro.addresslist.tools.NetUtil;
import com.pro.addresslist.ui.common.BaseActivity;
import com.pro.addresslist.ui.view.PopMenu;


public class managementActivity extends BaseActivity implements OnItemClickListener {
	private List<Object[]> listitm=new ArrayList<Object[]>();
	private PopMenu pop;
	private PopMenu popp;
	private SimpleAdapter adapter=null;
	private int state = 1;
	private  List<Map<String,Object>> listitems= new ArrayList<Map<String,Object>>();
	@Override
	public int getLayout() {
		return R.layout.management;
	}
	public class ReceiveBroadCast extends BroadcastReceiver 
	{ 
	  
	        @Override 
	        public void onReceive(Context context, Intent intent) 
	        { 
	        	String action = intent.getAction();
	        	if(action.equals("lbs_SelectContactNumBydy")){
	        		//得到广播中得到的数据，并显示出来 
	        		String num = (String) intent.getSerializableExtra("parameter");
	        		Log.i("libs", num+"tt");
	        		String dy = (String) intent.getSerializableExtra("dy");
	        		if(dy.equals("地域")){
	        			listitems.get(2).put("num", num);
	        			Log.i("libs", "地域"+num);
	        		}
	        		adapter.notifyDataSetChanged();
	        	}
	        	if(action.equals("lbs_SelectContactNumBygk")){
	        		//得到广播中得到的数据，并显示出来 
	        		String num = (String) intent.getSerializableExtra("parameter");
	        		Log.i("libs", num+"tt");
	        		String gk = (String) intent.getSerializableExtra("gk");
	        		if(gk.equals("内部")){
	        			listitems.get(0).put("num", num);
	        			Log.i("libs", "内部"+num);
	        		}
	        		if(gk.equals("外部")){
	        			listitems.get(1).put("num", num);
	        			Log.i("libs", "外部"+num);
	        		}
	        		adapter.notifyDataSetChanged();
	        	}
	        } 
	} 
	@Override
	public void OnActCreate(Bundle savedInstanceState) {
		setTtile("分类管理");
		setLeftImageBack();
		setViewClick(R.id.iv_title_right_image);
		//主页listview布局
		ListView  listview = (ListView)findViewById(R.id.listview1);
        int[] imageld = new int[] {R.drawable.icon_internal,R.drawable.icon_external,R.drawable.icon_area};
       
        String [] title = new String[]{"内部关系","外部关系","地域关系"};
        for(int i= 0; i < imageld.length;i++){
        	Map<String,Object> map = new HashMap<String,Object>();
        	map.put("image", imageld[i]);
        	map.put("title", title[i]);
        	map.put("num", "");
        	listitems.add(map);
        }
        
         adapter = new SimpleAdapter(this,listitems, R.layout.items, new String[]
        		{"title","image","num"},new int[] {
        		R.id.title,R.id.image,R.id.num
        		});
        listview.setAdapter(adapter);
        
        /**************************/
        listview.setOnItemClickListener(this);
        ReceiveBroadCast receiveBroadCast = new ReceiveBroadCast(); 
        IntentFilter filter = new IntentFilter(); 
        filter.addAction("lbs_SelectContactNumBydy");
        filter.addAction("lbs_SelectContactNumBygk");
        registerReceiver(receiveBroadCast, filter);
        ContactInfoBLL.SelectContactNumBydy(mContext, "地域");
        ContactInfoBLL.SelectContactNumBygk(mContext, "内部");
        ContactInfoBLL.SelectContactNumBygk(mContext, "外部");
       }
	@Override
	public void OnViewClick(View v) {

		}
	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		// TODO Auto-generated method stub
		switch (arg2) {
		case 0:
			if(NetUtil.isNetworkAvailable(mContext)){
			Intent int0=new Intent(this,externalActivity.class);
			int0.putExtra("action", "gk");
			int0.putExtra("name", "内部关系");
			int0.putExtra("id", "3");
			int0.putExtra("relation", "关系归口-内部");
			startActivity(int0);
			}
			else {
				Toast.makeText(mContext, "当前没有网络哟，请稍后再试！", Toast.LENGTH_LONG).show();
			}
			break;
		case 1:
			if(NetUtil.isNetworkAvailable(mContext)){
			Intent int1=new Intent(this,externalActivity.class);
			int1.putExtra("action", "gk");
			int1.putExtra("name", "外部关系");
			int1.putExtra("id", "4");
			int1.putExtra("relation", "关系归口-外部");
			startActivity(int1);
			}
			else {
				Toast.makeText(mContext, "当前没有网络哟，请稍后再试！", Toast.LENGTH_LONG).show();
			}
			break;
		case 2:
			if(NetUtil.isNetworkAvailable(mContext)){
			Intent int2=new Intent(this,externalActivity.class);
			int2.putExtra("action", "dy");
			int2.putExtra("name", "地域关系");
			int2.putExtra("id", "2");
			int2.putExtra("relation", "地域");
			startActivity(int2);
			}
			else {
				Toast.makeText(mContext, "当前没有网络哟，请稍后再试！", Toast.LENGTH_LONG).show();
			}
		break;
		default:
			break;
		}
	}
	
	}


